package com.ljyhhh.SmartCity.activity.fragment.orientationv

import android.Manifest.permission.ACCESS_FINE_LOCATION
import android.content.Context
import android.content.pm.PackageManager
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.fragment.app.Fragment
import com.baidu.location.BDAbstractLocationListener
import com.baidu.location.BDLocation
import com.baidu.location.LocationClient
import com.baidu.location.LocationClientOption
import com.ljyhhh.SmartCity.R
import kotlinx.android.synthetic.main.activity_baidu_orientation.*

class BaiduOrientation : Fragment() {

    lateinit var mLocationClient: LocationClient
    private lateinit var myContext:Context
    private val listener=MyLocationListener()
    override fun onAttach(context: Context) {
        super.onAttach(context)
        this.myContext= context
    }
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.activity_baidu_orientation,container,false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        if (ActivityCompat.checkSelfPermission(myContext, ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            activity?.let {
                ActivityCompat.requestPermissions(
                    it,
                    arrayOf(ACCESS_FINE_LOCATION),
                    1
                )
            }
        }

        LocationClient.setAgreePrivacy(true)
        mLocationClient = LocationClient(myContext)
        mLocationClient.registerLocationListener(listener)
        button_get_orientation.setOnClickListener {
            val option =  LocationClientOption()
            option.setIsNeedAddress(true)
            mLocationClient.locOption = option

            mLocationClient.start()
            Toast.makeText(myContext,"还没写,",Toast.LENGTH_SHORT).show()

        }
    }
    fun logMsg(str: String?) {
        try {
            if (text_orientation != null) {
                Thread {
                    text_orientation.post {

                        text_orientation.text = str

                    }
                }.start()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }


    inner class MyLocationListener : BDAbstractLocationListener() {
        override fun onReceiveLocation(location: BDLocation) {
            //此处的BDLocation为定位结果信息类，通过它的各种get方法可获取定位相关的全部结果
            //以下只列举部分获取地址相关的结果信息
            //更多结果信息获取说明，请参照类参考中BDLocation类中的说明
            val addr = location.addrStr //获取详细地址信息
//            val country = location.country //获取国家
//            val province = location.province //获取省份
//            val city = location.city //获取城市
//            val district = location.district //获取区县
//            val street = location.street //获取街道信息
//            val adcode = location.adCode //获取adcode
            val town = location.town //获取乡镇信息
//            text_orientation.text=addr
//            if(location.locType ==BDLocation.TypeGpsLocation || location.locType ==BDLocation.TypeNetWorkLocation){
//                navigateTo(bdLocation);
//            }
            logMsg(addr)
        }
    }

//    private fun navigateTo(bdLocation: BDLocation) {
//        if (isFirstLocate) {
//            val ll = LatLng(bdLocation.latitude, bdLocation.longitude)
//            val update: MapStatusUpdate = MapStatusUpdateFactory.newLatLng(ll)
//            baiduMap.animateMapStatus(update)
//            isFirstLocate = false
//        }
//    }



}